<template>
    <!-- detail-Navbar -->
    <div class="detail-Nav">
        <van-nav-bar title="商品详情" left-text="" left-arrow @click-left="onClickLeft" />
    </div>
    <div class="goods" v-if="!unNot">
        <!-- 轮播 -->
        <van-swipe class="goods-swipe" :autoplay="3000">
            <van-swipe-item v-for="item in album" :key="item.id">
                <img :src="item.picture" height="414">
            </van-swipe-item>
        </van-swipe>
        <!-- cell单元格 -->
        <van-cell-group>
            <van-cell>
                <template #title>
                    <span class="goods-newGoods">新品</span>
                    <div class="goods-price">
                        <span class="price-detail">￥</span>
                        {{ goods.price }}
                        <span class="price-spec">{{ goods.spec }}</span>
                    </div>
                    <div class="goods-title">
                        <span class="title-detail">{{ goods.name }}</span>
                    </div>
                </template>
            </van-cell>
            <!-- 运费小字提示 -->
            <van-cell class="goods-carry">
                <template #title>
                    <van-col span="10">运费：10</van-col>
                    <van-col span="14">剩余：{{ goods.stock }}</van-col>
                </template>
            </van-cell>
            <!-- tip -->
        </van-cell-group>
        <van-cell-group class="goods-cell">
            <van-cell>
                <template #title>
                    <span class="van-cell-text">发货 陕西宝鸡</span>
                </template>
            </van-cell>
            <van-cell>
                <template #title>
                    <span class="van-cell-text">保障 坏单包赔·假一赔四·极速退款</span>
                </template>
            </van-cell>
            <van-cell>
                <template #title>
                    <span class="van-cell-text">参数 品牌：枝纯 价格：100-200</span>
                </template>
            </van-cell>
        </van-cell-group>
        <div class="goods-detailTitle">
            —— 宝贝详情 ——
        </div>
        <div class="goods-description" v-html="goods.description"></div>
        <!-- 底部按钮-->
        <van-action-bar>
            <van-action-bar-icon icon="chat-o" text="客服" @click="noService" />
            <van-action-bar-icon icon="cart-o" :badge="cartNum()||null" text="购物车" @click="getCateCart()" />
            <van-action-bar-button type="warning" text="加入购物车" @click="addCart" />
            <van-action-bar-button type="danger" text="立即购买" @click="noBuy" />
        </van-action-bar>
    </div>
    <div class="goods-not" v-else>商品不存在</div>
</template>

<script setup>
import { onMounted, reactive, ref, } from 'vue';
import {storeToRefs} from 'pinia'
import { getGoodsDetail, getGoodsAlbum } from '../api';
import { showToast } from 'vant';
import router from '../router';
import getCart from '../stores/cart';
const store = getCart()
// console.log(getCart(),666);

const { cart,cartNum, useCartStore } =store 



const goods = reactive({})
const album = ref([])
const unNot = ref(false)

// 声明传值
const props = defineProps({
    id: String
})

//生命周期开始时就调用方法
onMounted(() => {
    GoodsDetail()
})

//navBar返回上级
const onClickLeft = () => history.back()

//加载详情页
const GoodsDetail = async () => {
    // console.log(props.id);
    const data1 = await getGoodsDetail({ id: props.id })
    if (!data1.id) {
        unNot.value = true
        return
    }
    const data2 = await getGoodsAlbum({ goods_id: props.id })
    //都为真时，推入图片
    if (data2.length === 0 && data1.picture !== '') {
        data2.push({ picture: data1.picture })
    }
    //将data1的值复制给goods并返回（返回修改后的值）
    Object.assign(goods, data1)
    // console.log(goods,111);

    album.value = data2
}

// 客服，立即购买
const noService = () => {
    showToast({
        message: '功能正在升级中~'
    })
}

const noBuy = () => {
    showToast({
        message: '购买成功！'
    })
}

//push到购物车页面
const getCateCart = () => {
    router.push({ name: 'cart' })
}

//添加到购物车
const addCart = () => {
    useCartStore({ id: props.id, num: 1, checked: true })
    showToast({
        message: '添加成功'
    })
}
</script>

<style scoped>
.detail-Nav {
    --van-nav-bar-background: #ff8001;
    --van-nav-bar-title-text-color: #fff;
    --van-nav-bar-icon-color: #fff;
}

.goods {
    text-align: center;
    padding-bottom: 50px;
}

/* 轮播样式 */
.goods-swipe {
    img {
        width: 100%;
        display: block;
    }
}

.goods-newGoods {
    display: block;
    width: 30px;
    padding: 0 5px;
    border-radius: 10px;
    color: #fff;
    background-color: #f44;
}

.goods-title {
    text-align: left;

    .title-detail {
        font-size: 14px;
    }
}

.goods-price {
    color: #f44;
    text-align: left;
    font-size: 20px;
}

.price-detail {
    font-size: 12px;
}

.price-spec {
    font-size: 12px;
    color: #999;
}

.goods-carry {
    color: #999;
    font-size: 12px;
    padding: 5px 15px;
}

.goods-description {
    width: 95%;
    margin: 0 auto;
    padding-bottom: 20px;
    font-size: 14px;
}

:deep(img) {
    max-width: 100%;
    height: auto;
    display: block;
}

.goods-cell {
    :deep(.van-cell__title span) {
        float: left;
    }
}

:deep(.van-cell:after) {
    border: none;
}


.goods-not {
    padding-top: 48px;
    text-align: center;
    font-size: 28px;
}


:deep(.van-col) {
    float: left;
}

:deep(.van-col--14) {
    width: 58%;
}

.goods-detailTitle {
    padding: 20px 0;
}
</style>